package com.tianpengtech.common.plugin.shiro.tag;

import com.tianpengtech.admin.role.RoleDao;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.role.Role;
import com.tianpengtech.common.util.XxxUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;


public class ShiroTag {


    public static boolean isPermission(String permission){

        Subject subject = SecurityUtils.getSubject();
        return subject.isPermitted(permission);
    }


    public static boolean isRole(String role){
        Subject subject = SecurityUtils.getSubject();
        return subject.hasRole(role);
    }



    /*
        获取登录信息
     */
    public static Account readSessionInfo(){
        Account account =XxxUtil.readSessionInfo();
        return account;
    }

    public static String readLoginAccount(){
        Account account  = readSessionInfo();
        return (account==null)?"": account.getStr(Account.USERNAME);
    }

    public static String readLoginAccountRole(){
        Account account  = readSessionInfo();
        Role role = RoleDao.ME.readRole(account.getStr(Account.ROLE));
        return (role==null)?"": role.getStr(Role.NAME);
    }

    public static String readLoginAccountRoleCode(){
        Account account  = readSessionInfo();
        Role role = RoleDao.ME.readRole(account.getStr(Account.ROLE));
        return (role==null)?"": role.getStr(Role.CODE);
    }

    public static String readAccountRole(String roleCode){
        Role role = RoleDao.ME.readRole(roleCode);
        return role==null?"":role.getStr(Role.NAME);
    }
}
